package com.mnsq.permission.service.impl;

import com.mnsq.permission.service.SysService;
import com.mnsq.user.bean.entity.User;
import com.mnsq.user.dao.UserRepository;
import com.vwkit.common.bean.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.awt.print.Pageable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 周俭
 */
@Service
public class SysServiceImpl implements SysService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public Result listUser(String userName, Integer page, Integer size) {
        if (page == null || page < 0) {
            page = 1;
        }
        if (size == null || size < 20) {
            size = 20;
        }
        if (size > 500) {
            size = 500;
        }
        PageRequest pageRequest = PageRequest.of(page - 1, size, Sort.by("createTime").descending());
        if (StringUtils.isBlank(userName)) {
            userName = "%";
        }
        Page<User> pageRes = userRepository.findUserByPetNameLike(userName, pageRequest);
        Map<String, Object> map = new HashMap<>();
        map.put("content",pageRes.getContent());
        map.put("pageNum",page);
        map.put("totalPages",pageRes.getTotalPages());
        map.put("totalSize",pageRes.getTotalElements());
        return Result.success(map);
    }
}
